package com.szcinda.service.dto.orderFee;

import com.szcinda.repository.OrderFee;
import com.szcinda.repository.OrderFeeRepository;
import com.szcinda.service.SnowFlakeFactory;
import com.szcinda.service.TypeStringUtils;
import com.szcinda.service.dto.log.LogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class OrderFeeServiceImpl implements OrderFeeService {
    private final OrderFeeRepository orderFeeRepository;
    private final LogService logService;
    private final SnowFlakeFactory snowFlakeFactory;

    public OrderFeeServiceImpl(OrderFeeRepository orderFeeRepository, LogService logService) {
        this.orderFeeRepository = orderFeeRepository;
        this.logService = logService;
        this.snowFlakeFactory = SnowFlakeFactory.getInstance();
    }

    @Override
    public void lockFee(List<String> ids, String userId, String type, String feeName) {
        List<OrderFee> orderFees = orderFeeRepository.findByOrderIdIn(ids);
        orderFees.forEach(orderFee -> {
            if (orderFee.getType().equals(type) && orderFee.getFeeName().equals(feeName)) {
                orderFee.setStatus(TypeStringUtils.feeLockStatus);
            }
        });
        orderFeeRepository.save(orderFees);
    }

    @Override
    public void unlockFee(List<String> ids, String userId, String type, String feeName) {
        List<OrderFee> orderFees = orderFeeRepository.findByOrderIdIn(ids);
        orderFees.forEach(orderFee -> {
            if (orderFee.getType().equals(type) && orderFee.getFeeName().equals(feeName)) {
                orderFee.setStatus(TypeStringUtils.feeUnLockStatus);
            }
        });
        orderFeeRepository.save(orderFees);
    }

    @Override
    public void updateFee(String rowId, String userId, String type, String feeName, double feeCount, String currency) {
        List<OrderFee> orderFees = orderFeeRepository.findByOrderId(rowId);
        Optional<OrderFee> optional = orderFees.stream().filter(orderFee -> orderFee.getType().equals(type) && orderFee.getFeeName().equals(feeName))
                .findFirst();
        if (optional.isPresent()) {
            OrderFee orderFee = optional.get();
            orderFee.setFeeCount(feeCount);
            orderFee.setCurrency(currency);
            orderFeeRepository.save(orderFee);
        } else {
            OrderFee create = new OrderFee();
            create.setId(snowFlakeFactory.nextId("OF"));
            create.setOrderId(rowId);
            create.setCurrency(currency);
            create.setFeeCount(feeCount);
            create.setType(type);
            create.setFeeName(feeName);
            create.setStatus(TypeStringUtils.feeUnLockStatus);
            orderFeeRepository.save(create);
        }
    }

    @Override
    public void lockFee(String feeId, String userId) {
        OrderFee orderFee = orderFeeRepository.findById(feeId);
        Assert.isTrue(orderFee != null, "请先修改保存费用后再进行锁定");
        orderFee.setStatus(TypeStringUtils.feeLockStatus);
        orderFeeRepository.save(orderFee);
    }

    @Override
    public void unlockFee(String feeId, String userId) {
        OrderFee orderFee = orderFeeRepository.findById(feeId);
        Assert.isTrue(orderFee != null, "请先修改保存费用后再进行锁定");
        orderFee.setStatus(TypeStringUtils.feeUnLockStatus);
        orderFeeRepository.save(orderFee);
    }
}
